<?php 
class comment extends CI_Controller
{

	function __construct()
	 {
	    parent::__construct();	
	    $this->load->helper(array("url","form"));

	 }
	 
	 function index()
	 {
	 	/*     分页代码          */
	    $num = 10;    //每页显示数量
		if($this->uri->segment(3) == FALSE)
		{
			$offset = 0;
		}else{
			if($this->uri->segment(3) ==1)
			{
				$offset=0;
			}else{
			$offset = $this->uri->segment(3)*$num-$num;   //计算篇移量
			} 
		}
		$counts = $this->db->count_all('comment');
	    $count_page = ceil($counts/$num);
        $data['count_page'] = $count_page;  
        $user = $this->session->userdata('username');
        if(!$user)
        {
        	$user = "L";
        }
        $data['user'] = $user;
        
	 	$this->db->select('*');
	    $this->db->limit($num, $offset);   
	    $this->db->order_by('id','desc');

	    $data['comments'] = $this->db->get('comment')->result_array();
	 	
	 	//$data['count_page'] = 0;
	 	$this->load->view("manage/header");
	 	$this->load->view("comment/index",$data);
	 	$this->load->view("manage/footer");
	 }
	 
	 /*
	  *   通过ajax在后台进行回复
	  */
	 function reply()
	 {
	 	   //$cid = $this->uri->segment(3);
	 	   $data = $this->input->post();
	 	   
	 	   //$data["pid"] = $cid;
	 	   $data["nickname"] = "L";
	 	   $data["email"] = "L@g.cn";
	 	   $data["created"] = time();
	 	   $data['body'] =htmlspecialchars($data['body']);
	 	   $this->db->insert("comment", $data);
	 	   //redirect("comment/index","refresh");
	 }
	 
	 function drop()
	 {
	 	$cid = $this->uri->segment(4);
	 	$this->db->where('cid',$cid)->delete("comment");
	 	redirect("comment/index", "refresh");
	 }
	 
     public function batchdrop()
	{
		$post = $this->input->post('cbSelect');
		if($post)
		{
			$count = count($post);
			for($i=0;$i<$count;$i++)
			{
				$this->db->where('cid',$post[$i])->delete('comment');
			}
		}
		redirect('comment/index',"refresh");
	}
	
	public function batchdeactivate()
	{
		$post = $this->input->post('cbSelect');
		if($post)
		{
			$count = count($post);
			for($i=0;$i<$count;$i++)
			{
				$data = array('is_show'=>0);
				$this->db->where('cid',$post[$i])->update('comment',$data);
			}
		}
		redirect('comment/index',"refresh");
	}
	
    public function batchactivate()
	{
		$post = $this->input->post('cbSelect');
		if($post)
		{
			$count = count($post);
			for($i=0;$i<$count;$i++)
			{
				$data = array('is_show'=>1);
				$this->db->where('cid',$post[$i])->update('comment',$data);
			}
		}
		redirect('comment/index',"refresh");
	}
	
	public function isactivate()
	{
		$id = $this->uri->segment(3);
		if($id)
		{
			$id = intval($id);
			$data = array('is_show'=>1);
			$this->db->where('cid',$id)->update('comment',$data);
		}
		redirect('comment/index');
	}
}	 